package com.qn.common.knife4j.config;

import com.github.xiaoymin.knife4j.spring.extension.OpenApiExtensionResolver;
import com.qn.common.knife4j.support.SwaggerProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import javax.annotation.Resource;

/**
 * knife4j配置
 *
 * @author QN
 * @date 2022/02/23
 */
@Configuration
@EnableConfigurationProperties(value = {SwaggerProperties.class})
public class Knife4jConfiguration {
    /**
     * 配置属性
     */
    @Resource
    private SwaggerProperties properties;

    /**
     * 引入Knife4j提供的扩展类
     */
    @Resource
    private OpenApiExtensionResolver openApiExtensionResolver;

    @Bean
    public Docket frontApi() {
        return new Docket(DocumentationType.OAS_30)
                .groupName(properties.getFront().getGroupName())
                .apiInfo(frontApiInfo())
                .select()
                .paths(PathSelectors.any())
                // 指定扫描的包
                .apis(RequestHandlerSelectors.basePackage(properties.getFront().getBasePackage()))
                .build()
                //赋予插件体系
                .extensions(openApiExtensionResolver.buildExtensions(properties.getFront().getGroupName()));
    }

    /**
     * 前台API信息
     */
    private ApiInfo frontApiInfo() {
        return new ApiInfoBuilder()
                .title(properties.getFront().getTitle())
                .description(properties.getFront().getDescription())
                .version(properties.getFront().getVersion())
                .contact(    // 添加开发者的一些信息
                        new Contact(
                                properties.getFront().getContactName(),
                                properties.getFront().getContactUrl(),
                                properties.getFront().getContactEmail()))
                .build();
    }

    /**
     * 后台API信息
     */
    private ApiInfo backApiInfo() {
        return new ApiInfoBuilder()
                .title(properties.getBack().getTitle())
                .description(properties.getBack().getDescription())
                .version(properties.getBack().getVersion())
                .contact(    // 添加开发者的一些信息
                        new Contact(
                                properties.getBack().getContactName(),
                                properties.getBack().getContactUrl(),
                                properties.getBack().getContactEmail()))
                .build();
    }

    /**
     * 后台API
     */
    @Bean
    public Docket backApi() {
        return new Docket(DocumentationType.OAS_30)
                .groupName(properties.getBack().getGroupName())
                .apiInfo(backApiInfo())
                .select()
                .paths(PathSelectors.any())
                // 指定扫描的包
                .apis(RequestHandlerSelectors.basePackage(properties.getFront().getBasePackage()))
                .build()
                //赋予插件体系
                .extensions(openApiExtensionResolver.buildExtensions(properties.getFront().getGroupName()));
    }

}